package zero.tech.games.logic.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import zero.tech.core.data.SimpleUser;
import zero.tech.games.common.data.RoomNode;
import zero.tech.games.common.data.UserLastRoom;
import zero.tech.games.logic.common.GameBase;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.common.RoomCache;
import zero.tech.games.logic.data.DissolutionData;
import zero.tech.games.logic.data.notify.NotifyBigCalculate;
import zero.tech.games.logic.data.notify.NotifyBigReset;
import zero.tech.games.logic.data.notify.NotifyDissolutionAnswerArrived;
import zero.tech.games.logic.data.notify.NotifyDissolutionApplyArrived;
import zero.tech.games.logic.data.notify.NotifyDissolutionRoomArrived;
import zero.tech.games.logic.data.notify.NotifyEnterAutoDeposit;
import zero.tech.games.logic.data.notify.NotifyExitAutoDeposit;
import zero.tech.games.logic.data.notify.NotifySmallCalculate;
import zero.tech.games.logic.data.notify.NotifySmallReset;
import zero.tech.games.logic.data.notify.NotifySomeOneJoin;
import zero.tech.games.logic.data.notify.NotifySomeOneLeave;
import zero.tech.games.logic.data.notify.NotifySomeOneOffline;
import zero.tech.games.logic.data.notify.NotifySomeOneOnline;
import zero.tech.games.logic.data.notify.NotifySomeOneReady;
import zero.tech.games.logic.data.panel.BigPanel;
import zero.tech.games.logic.data.panel.SmallPanel;
import zero.tech.games.logic.data.res.ResDissolution;
import zero.tech.games.logic.data.res.ResDissolutionData;
import zero.tech.games.logic.data.res.ResExitAutoDeposit;
import zero.tech.games.logic.data.res.ResReconnet;
import zero.tech.games.logic.data.res.ResSeat;
import zero.tech.wsnet.controller.LogicController;

public class PckUtil {

	public static NotifyBigCalculate packageNotifyBigCalculate(Room room) {
		NotifyBigCalculate notifyBigCalculate=new NotifyBigCalculate();
		RoomCache roomCache= room.getRoomCache();
		BigPanel bigPanel = roomCache.getBigPanel();
		notifyBigCalculate.setBigPanel(bigPanel);
		return notifyBigCalculate;
	}
	
	public static NotifySmallCalculate packageNotifySmallCalculate(Room room) {
		NotifySmallCalculate notifySmallCalculate = new NotifySmallCalculate();
		RoomCache roomCache= room.getRoomCache();
		int room_times=room.getRoom_times();
		SmallPanel smallPanel = roomCache.getSmallPanels().get(room_times);
		notifySmallCalculate.setSmallPanel(smallPanel);
		return notifySmallCalculate;
	}
	
	public static NotifySomeOneJoin packageNotifySomeOneJoin(Room room, LogicController online) {
		NotifySomeOneJoin notifySomeOneJoin = new NotifySomeOneJoin();
		notifySomeOneJoin.setSimpleUser(online.getSimpleUser());
		notifySomeOneJoin.setPos(LogicUtil.getPosByUid(room, online.getId()));
		notifySomeOneJoin.setGold(online.getGold());
		notifySomeOneJoin.setWin(online.getWin());
		return notifySomeOneJoin;
	}

	public static NotifySomeOneReady packageNotifySomeOneReady(LogicController controller) {
		NotifySomeOneReady notifySomeOneReady=new NotifySomeOneReady();
		notifySomeOneReady.setUid(controller.getId());
		return notifySomeOneReady;
	}
	public static ResSeat packageResSeat(LogicController logicController) {
		ResSeat resSeat=new ResSeat();
		resSeat.setAi(logicController.getSimpleUser().getAi());
		resSeat.setGold(logicController.getGold());
		resSeat.setHead_img(logicController.getSimpleUser().getHead_img());
		resSeat.setNick(logicController.getSimpleUser().getNick());
		resSeat.setSex(logicController.getSimpleUser().getSex());
		resSeat.setUid(logicController.getSimpleUser().getUid());
		resSeat.setWin(logicController.getWin());
		return resSeat;
	}

	public static NotifySomeOneLeave packageNotifySomeOneLeave(LogicController logicController) {
		NotifySomeOneLeave notifySomeOneLeave = new NotifySomeOneLeave();
		notifySomeOneLeave.setUid(logicController.getId());
		return notifySomeOneLeave;
	}

	public static NotifyDissolutionApplyArrived packageNotifyDissolutionApplyArrived(int uid,DissolutionData dissolutionData) {
		NotifyDissolutionApplyArrived notifyDissolutionApplyArrived=new NotifyDissolutionApplyArrived();
		notifyDissolutionApplyArrived.setResDissolutionData(packageResDissolutionData(dissolutionData));
		return notifyDissolutionApplyArrived;
	}

	public static NotifyDissolutionAnswerArrived packageNotifyDissolutionAnswerArrived(int uid, int agree) {
		NotifyDissolutionAnswerArrived notifyDissolutionAnswerArrived=new NotifyDissolutionAnswerArrived();
		notifyDissolutionAnswerArrived.setAgree(agree);
		notifyDissolutionAnswerArrived.setAnswer_uid(uid);
		return notifyDissolutionAnswerArrived;
	}

	public static ResDissolutionData packageResDissolutionData(DissolutionData dissolutionData) {
		ResDissolutionData resDissolutionData=new ResDissolutionData();
		resDissolutionData.getDissolutions().putAll(dissolutionData.getDissolutions());
		long now=System.currentTimeMillis();
		int left=(int) (dissolutionData.getDissolution_total_time()-(now-dissolutionData.getDissolution_apply_time())/1000);
		resDissolutionData.setAuto_answer_left_time(left);
		resDissolutionData.setApply_uid(dissolutionData.getApply_uid());
		return resDissolutionData;
	}

	public static UserLastRoom packageUserLastRoom(Room room, int uid) {
		RoomNode roomNode = room.getRoomNode();
		UserLastRoom userLastRoom=new UserLastRoom(uid,roomNode.getHost(),roomNode.getPort(),roomNode.getService_id(),roomNode.getRoom_id(),room.getGame_id(),roomNode.getClub_id());
		return userLastRoom;
	}
	
	public static Map<Integer,SimpleUser> packageSeats(Room room) {
		Map<Integer,SimpleUser> seats=new HashMap<Integer, SimpleUser>();
		Map<Integer,Integer> seatmap=room.getSeats();
		for(Entry<Integer, Integer> entry:seatmap.entrySet()) {
			Integer uid=entry.getValue();
			Integer pos=entry.getKey();
			if(uid!=null) {
				LogicController logicController=room.getAllControllers().get(uid);
				SimpleUser simpleUser=logicController.getSimpleUser();
				seats.put(pos, simpleUser);
			}
		}
		return seats;
	}

	public static NotifyDissolutionRoomArrived packageNotifyDissolutionRoomArrived(int type) {
		NotifyDissolutionRoomArrived notifyDissolutionRoomArrived=new NotifyDissolutionRoomArrived();
		notifyDissolutionRoomArrived.setType(type);
		return notifyDissolutionRoomArrived;
	}

	public static ResDissolution packageResDissolution(int agree) {
		ResDissolution resDissolution =new ResDissolution();
		resDissolution.setAgree(agree);
		return resDissolution;
	}

	public static void packageResReconnect(Room room, LogicController online, ResReconnet resReconnet) {
		RoomCache roomCache = room.getRoomCache();
		GameBase gameBase = room.getGameBase();
		resReconnet.setGame_id(room.getGame_id());
		resReconnet.setMax_player(room.getMax_player());
		resReconnet.setNow(System.currentTimeMillis());
		resReconnet.setRoom_id(room.getRoom_id());
		resReconnet.setRoom_status(room.getRoom_status());
		resReconnet.setConfigStr(room.getConfigStr());
		Map<Integer,Integer> map=room.getSeats();
		for(Entry<Integer,Integer> entry:map.entrySet()) {
			Integer pos=entry.getKey();
			Integer uid=entry.getValue();
			if(uid!=null) {
				resReconnet.getRes_seats().put(pos,PckUtil.packageResSeat(room.getAllControllers().get(uid)));
			}
		}
		resReconnet.setTotal_times(room.getGameConfig().getTotal_times());
		resReconnet.setRoom_times(room.getRoom_times());
		resReconnet.getReady_uids().addAll(gameBase.getReadys().keySet());
		resReconnet.getOfflines().addAll(roomCache.getOfflines());
		resReconnet.getWatchers().addAll(roomCache.getWatchers());
		resReconnet.getTaotais().addAll(roomCache.getTaotais());
		DissolutionData dissolutionData=roomCache.getDissolutionData();
		//有解散数据
		if(!dissolutionData.getDissolutions().isEmpty()) {
			resReconnet.setResDissolution(PckUtil.packageResDissolutionData(dissolutionData));
		}
	}

	public static NotifySomeOneOffline packageNotifySomeOneOffline(int uid) {
		NotifySomeOneOffline notifySomeOneOffline = new NotifySomeOneOffline();
		notifySomeOneOffline.setUid(uid);
		return notifySomeOneOffline;
	}

	public static NotifySomeOneOnline packageNotifySomeoneOnline(int uid) {
		NotifySomeOneOnline notifySomeOneOnline = new NotifySomeOneOnline();
		notifySomeOneOnline.setUid(uid);
		return notifySomeOneOnline;
	}
	
	public static NotifyEnterAutoDeposit packageNotifyEnterAutoDeposit(int uid, long now) {
		NotifyEnterAutoDeposit notifyEnterAutoDeposit = new NotifyEnterAutoDeposit();
		notifyEnterAutoDeposit.setUid(uid);
		notifyEnterAutoDeposit.setTime(now);
		return notifyEnterAutoDeposit;
	}

	public static NotifyExitAutoDeposit packageNotifyExitAutoDeposit(int uid, long time) {
		NotifyExitAutoDeposit notifyExitAutoDeposit = new NotifyExitAutoDeposit();
		notifyExitAutoDeposit.setUid(uid);
		notifyExitAutoDeposit.setTime(time);
		return notifyExitAutoDeposit;
	}

	public static ResExitAutoDeposit packageResExitAutoDeposit(LogicController controller) {
		ResExitAutoDeposit resExitAutoDeposit = new ResExitAutoDeposit();
		resExitAutoDeposit.setUid(controller.getId());
		return resExitAutoDeposit;
	}

	public static NotifySmallReset packageNotifySmallReset(Room room) {
		RoomCache roomCache = room.getRoomCache();
		NotifySmallReset notifySmallReset = new NotifySmallReset();
		notifySmallReset.getOfflines().addAll(roomCache.getOfflines());
		notifySmallReset.getWatchers().addAll(roomCache.getWatchers());
		notifySmallReset.getTaotais().addAll(roomCache.getTaotais());
		return notifySmallReset;
	}

	public static NotifyBigReset packageNotifyBigReset(Room room, List<Integer> kick_uids) {
		RoomCache roomCache = room.getRoomCache();
		NotifyBigReset notifyBigReset = new NotifyBigReset();
		notifyBigReset.getOfflines().addAll(roomCache.getOfflines());
		notifyBigReset.getWatchers().addAll(roomCache.getWatchers());
		notifyBigReset.getTaotais().addAll(roomCache.getTaotais());
		notifyBigReset.getKicks().addAll(kick_uids);
		return notifyBigReset;
	}

}
